#include "TictactoeLib.h"

void SwitchPlayer()
{
	int localMode;
	while(true)
	{
		printf("\n SELECT YOUR MODE (1 -PLAY GAME, 2 - REPLAY GAME, OTHERS - EXIT GAME):");
		scanf("%d", &localMode);
		switch(localMode)
		{
		case 1 : InputNamePlayer();
				ShowBoard();
				InGame();
				break;
		case 2 :
				ReplayGame();
				break;
		default : exit (EXIT_FAILURE);break;
		}
	}
}
void ReplayGame()
{	 
	int m = 0;
	int localSizeList;
	ReadRMS();
	localSizeList = s_lGlobalDataRead.size();
	for (Mylist::const_iterator i = s_lGlobalDataRead.begin(); i != s_lGlobalDataRead.end(); ++i)
	{
		if(m%s_iGlobalSizeBoard == 0)
		{
			printf("\n");
		}
		if(m % (s_iGlobalSizeBoard*s_iGlobalSizeBoard) == 0)
		{
			printf("\n Current Step: %d\n", (m/((s_iGlobalSizeBoard*s_iGlobalSizeBoard))+1));
		}
		if(*i == 'O')
		{
			printf(" O");
		}
		else if(*i == 'X')
		{
			printf(" X");
		}
		else if(*i == 'T')
		{
			printf(" T");
		}
	m++;
	if(m == localSizeList-1)
		break;
	}
	printf("\n");
}
void ReadRMS()
{
	int temp = 0;
    FILE * pFile;
	pFile = fopen("save.bin","r+");
	char tmp;
 
	 while(true)
	{
	 temp++ ;
	 fscanf(pFile, "%c", &tmp);
	 s_lGlobalDataRead.push_back(tmp);
	 if(feof(pFile))
	 break;	 
	}
    fclose(pFile);
}
void SaveRMS()
{
    FILE * pFile;
	pFile = fopen ("save.bin","w+");
	for (Mylist::const_iterator i = s_lGlobalDataWite.begin(); i != s_lGlobalDataWite.end(); ++i)
	{
	 fprintf (pFile, "%c", *i);
	}
 
    fclose (pFile);
    printf("\nThe game was saved !!!\n");
}
void SaveCurrentState()
{
	for(int i =0; i < s_iGlobalSizeBoard; i++ )
		for(int j =0 ; j < s_iGlobalSizeBoard; j ++)
		{
			s_lGlobalDataWite.push_back(s_cGlobalBoard[i][j]);
		}
}
void ExitGame()
{
	int localMode;
	while(true)
	{
		printf("\n Press any key to save and exit game:");
		scanf("%d", &localMode);
		switch(localMode)
		{
		default :
				SaveRMS();
				exit (EXIT_FAILURE);
				break;
		}
	}
}
void InGame()
{
	char* localPosition = new char[];
	while(true)
	{
		printf("\n Your turn ");
		if(s_bGlobalSwithPlayer)
		{
			printf("  %s - O:" ,s_cGlobalPlayer1);
		}
		else
		{
			printf("  %s - X:" ,s_cGlobalPlayer2);
		}
		scanf("%s",localPosition);
		int i = localPosition[0] - '0';
		int j = localPosition[1] - '0';
		if(strlen(localPosition)>=3 || i > (s_iGlobalSizeBoard) || j > (s_iGlobalSizeBoard) || strlen(localPosition)<=1 || i <=0 || j <= 0 )
		{
			printf( "\n You went out the board");
			
			
		}
		else
		{
			i = i -1;
			j = j -1;
				if(s_cGlobalBoard[i][j] != 'O' && s_cGlobalBoard[i][j] != 'X')
				{
					s_cGlobalBoard[i][j] = s_bGlobalSwithPlayer ? 'O' : 'X';
					s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;
					SaveCurrentState();
					
				}
				else 
				{
					printf("This move invalid!\n");

				}
				if(IsCheckWon('O') || IsCheckWon('X'))
				{
					ShowBoard();
					printf("\nCongratulation %s",(!s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2));
					printf("\n You won!");
					ExitGame();
					break;
				}
			ShowBoard();
			

		}
	}
	delete localPosition ;
	delete s_cGlobalPlayer1;
	delete s_cGlobalPlayer2;

}

void ShowBoard()
{
	printf("\n\n");
	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			printf ("%c  ", s_cGlobalBoard[i][j]);
		printf("\n");
	}
}
void InputNamePlayer()
{
	printf("Enter Player 1 Name:");
	scanf("%s",s_cGlobalPlayer1 );
	printf("Enter Player 2 Name:");
	scanf("%s",s_cGlobalPlayer2 );
	
}
bool IsCheckWon(char a)
{
	if(CheckRow(a)||CheckColumn(a)||CheckCrossIncrease(a))
		return true;
	else return false;

}
int CheckRow(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[i][j] == a)
			{
				if(s_cGlobalBoard[i][j+1] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;
}
int CheckColumn(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[j][i] == a)
			{
				if(s_cGlobalBoard[j+1][i] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;

}
int CheckCrossIncrease(char a)
{
	int localResult = 0;
	int localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][s_iGlobalSizeBoard -i - 1] == a )
			if(s_cGlobalBoard[i][s_iGlobalSizeBoard - 1 - i ] == a )
			localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;
	}
	if(localResult)
		return localResult;
	else
	localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][i] == a)
				localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;

	}
	return localResult;
}
void TictactoeLib()
{

	printf("Welcome To Tic-tac-toe game! Play with your way!");
	printf("\n\n");
	printf("If you find any problem, please contact tho.lekhac@gameloft.com\n");
	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			s_cGlobalBoard[i][j] = 'T';
	s_bGlobalSwithPlayer = true;
	
	s_cGlobalPlayer1 = new char[];
	s_cGlobalPlayer2 = new char[];
	

}